

package com.yungu.swift.common.service.impl;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.miemiedev.mybatis.paginator.domain.PageBounds;
import com.github.miemiedev.mybatis.paginator.domain.PageList;
import com.yungu.swift.base.ResponseData;
import com.yungu.swift.base.mapper.IMapper;
import com.yungu.swift.base.model.PageVo;
import com.yungu.swift.base.model.admin.AdminParam;
import com.yungu.swift.base.service.impl.BaseServiceImpl;
import com.yungu.swift.common.dao.CommonSystemMessageMapper;
import com.yungu.swift.common.model.dto.CommonSystemMessageDto;
import com.yungu.swift.common.model.param.AdminNoticePageParam;
import com.yungu.swift.common.model.param.AdminNoticeParam;
import com.yungu.swift.common.service.CommonMessageStatusService;
import com.yungu.swift.common.service.CommonSystemMessageService;
import com.yungu.swift.constants.CommonConstant;
import com.yungu.swift.socket.push.model.PushContent;
import com.yungu.swift.socket.push.service.PushService;
import com.yungu.swift.system.sys.model.dto.SysUserDto;
import com.yungu.swift.system.sys.service.SysUserService;
import com.yungu.swift.utils.MapUtils;
import com.yungu.swift.utils.StringUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.support.TransactionTemplate;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class CommonSystemMessageServiceImpl extends BaseServiceImpl<CommonSystemMessageDto>
        implements CommonSystemMessageService {

    @Autowired
    private TransactionTemplate transactionTemplate;
    @Autowired
    private CommonSystemMessageMapper commonSystemMessageMapper;
    @Autowired
    private CommonMessageStatusService commonMessageStatusService;
    @Reference
    private SysUserService sysUserService;
    @Reference
    private PushService pushService;

    @Override
    protected IMapper<CommonSystemMessageDto> getMapper() {
        return commonSystemMessageMapper;
    }

    @Override
    public ResponseData<Integer> sysUnreadNum(AdminParam adminParam) {
        return ResponseData.buildSuccessResponse(commonSystemMessageMapper.sysUnreadNum(MapUtils.convertObjToMap(adminParam)));
    }

    @Override
    public ResponseData<PageVo<CommonSystemMessageDto>> sysQueryPage(AdminNoticePageParam pageParam) {
        //改变消息所有未读状态为已读
        Map<String,Object> paraMap = new HashMap<>();
        paraMap.put("status",CommonConstant.MSG_STATUS_HASREAD);
        paraMap.put("uuid",pageParam.getUserUuid());
        commonMessageStatusService.updSysMessageStatus(paraMap);
        PageBounds pageBounds = new PageBounds(pageParam.getPageNum(), pageParam.getPageSize());
        PageList<CommonSystemMessageDto> pageList = commonSystemMessageMapper.sysQueryPage(MapUtils.convertObjToMap(
                pageParam), pageBounds);
        PageVo<CommonSystemMessageDto> pageVo = new PageVo(pageParam.getPageNum(), pageParam.getPageSize(),
                pageList.getPaginator().getTotalCount());
        pageVo.setItems(pageList);
        return ResponseData.buildSuccessResponse(pageVo);
    }

    @Override
    public ResponseData<Boolean> saveOrUpdate(AdminNoticeParam adminNoticeParam) {
        CommonSystemMessageDto systemMessageDto = new CommonSystemMessageDto();
        BeanUtils.copyProperties(adminNoticeParam, systemMessageDto);
        // 新增
        if (StringUtils.isEmpty(systemMessageDto.getUuid())) {
            systemMessageDto.setUuid(StringUtils.buildUUID());
            systemMessageDto.setStatus(CommonSystemMessageDto.IS_SEND);
            systemMessageDto.setCreateOn(new Date());
            systemMessageDto.setCreateBy(adminNoticeParam.getUserUuid());
            //消息保存成功之后在记录 每个用户收到的消息状态 目前后台的系统消息是默认发给全部后台用户
            List<String> list = null;
            if (systemMessageDto.getSendObject() == CommonSystemMessageDto.SEND_OBJECT_ALL_SYS_USERS) {
                //获取所有用户
                Map<String, Object> map = MapUtils.build(4);
                map.put("status", 1);
                map.put("appid", adminNoticeParam.getAppid());
                ResponseData<List<SysUserDto>> responseData = sysUserService.list(map);
                if (responseData.isSuccess() && CollectionUtils.isNotEmpty(responseData.getData())) {
                    list = responseData.getData().stream().map(SysUserDto::getUuid).collect(Collectors.toList());
                }
            }
            final List<String> finalList = list;
            transactionTemplate.execute(transactionStatus -> {
                // 新增消息
                commonSystemMessageMapper.add(systemMessageDto);
                // 发送用户
                if (CollectionUtils.isNotEmpty(finalList)) {
                    commonMessageStatusService.addSysMessageStatus(systemMessageDto, finalList);
                }
                Map<String, Object> dataMap = MapUtils.build();
                dataMap.put("report", "平台有新通知");
                PushContent pushContent = PushContent.newInstance4Admin(adminNoticeParam.getAppid(), PushContent.SYSTEM_MESSAGE_NOTICE, dataMap);
                pushService.sendMessageByAdmin(pushContent);

                return Boolean.TRUE;
            });
        }
        // 修改
        else {
            systemMessageDto.setUpdateBy(adminNoticeParam.getUserUuid());
            systemMessageDto.setUpdateOn(new Date());
            commonSystemMessageMapper.edit(systemMessageDto);
        }
        return ResponseData.buildSuccessResponse(Boolean.TRUE);
    }
}